<?php


namespace App\Http\Controllers\User;


use App\Http\Controllers\Controller;
use App\Models\Account;
use App\Models\AccountFund;
use App\Models\PayOrder;
use Illuminate\Http\Request;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Log;


class PayController extends Controller
{
    public function wxPay(Request $request)
    {
        $amount = (int)$request->input('amount');
        $user = $request->user();
        $config = config('wechat');
        $app = Factory::payment($config);
        $order_no = 'wx' . date('yymmddhhmmss');
        $title = '慧连易运-智慧豆充值';
        $result = $app->order->unify([
            'body' => $title,
            'out_trade_no' => $order_no,
            'total_fee' => $amount,
            'trade_type' => 'JSAPI',
            'openid' => $user->openid,
        ]);
        if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
            $result = PayOrder::getJSAPIPaySign($config['app_id'], $result['prepay_id'], $config['key']);
            $result['order_no'] = $order_no;
            $order = new PayOrder();
            $order->fill([
                'user_id' => $user->id,
                'title' => $title,
                'order_no' => $order_no,
                'pay_type' => 'wx',
                'amount' => $amount,
                'status' => 0,
            ]);
            $order->save();
            return $this->response($result);
        } else {
            Log::error('微信支付签名失败:' . var_export($result, 1));
            abort(5075);
        }
    }

    public function wxPayNotify()
    {
        $config = config('wechat');
        $app = Factory::payment($config);
        return $app->handlePaidNotify(function ($message, $fail) {
            if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态，不代表支付状态
                $order = PayOrder::where('order_no', $message['out_trade_no'])->first();
                if ($message['result_code'] === 'SUCCESS') {
                    $account = Account::where('user_id', $order->user_id)->first();
                    $accountFund = new AccountFund();
                    $usable_amount = $order->amount + $account->usable_amount;
                    $amount = $order->amount + $account->amount;
                    $accountFund->fill([
                        'user_id'=>$account->user_id,
                        'account_id'=>$account->id,
                        'title'=>$order->title,
                        'rid'=>$order->order_no,
                        'type'=>1,
                        'amount'=>$order->amount,
                        'reckon'=>1,
                        'before_amount'=> $account->amount,
                        'later_amount'=> $amount,
                    ]);
                    $accountFund->save();
                    $account->usable_amount = $usable_amount;
                    $account->amount = $amount;
                    $account->save();
                    $order->status = 1;
                } else if ($message['result_code'] === 'FAIL') {
                    $order->status = 2;
                }
                $order->account_id = $account->id;
                $order->notify_data = json_encode($message);
                $order->save();
                return true;
            } else {
                return $fail('通信失败，请稍后再通知我');
            }
        });
    }
    public function checkPay(Request $request){
        $order = PayOrder::where('order_no', $request->input('order_no'))->first();
        return $this->response($order->status);
    }
}
